#include "trolly.h"
#include "ui_trolly.h"

trolly::trolly(QWidget *parent,int uid,bool isVIP) :
    QDialog(parent),
    ui(new Ui::trolly)
{
    m_uid = uid;
    m_isVIP = isVIP;
    ui->setupUi(this);
    myDatabase.getConnection();

}

trolly::~trolly()
{
    delete ui;
}

void trolly::on_Quit_clicked()
{
    this->~trolly();
}


void trolly::on_Refresh_clicked()
{
    static QSqlQueryModel *mode= new QSqlQueryModel(ui->tableView);
    mode->setQuery("SELECT tstore,tgoods,tcat,tprice,tnum,ttime FROM tb_trolly WHERE tuser ="+ QString::number(m_uid));

    mode->setHeaderData(0,Qt::Horizontal,tr("store"));
    mode->setHeaderData(1, Qt::Horizontal, tr("goods"));
    mode->setHeaderData(2, Qt::Horizontal, tr("cat"));
    mode->setHeaderData(3, Qt::Horizontal, tr("price"));
    mode->setHeaderData(4, Qt::Horizontal, tr("num"));
    mode->setHeaderData(5, Qt::Horizontal, tr("time"));

    ui->tableView->setModel(mode);
}


void trolly::on_Pay_clicked()
{
    QSqlQuery sql;
    sql.prepare("SELECT tgoods,tcat,tnum,tstore FROM tb_trolly WHERE tuser ="+ QString::number(m_uid));
    sql.exec();
    if(!sql.first()){
        QMessageBox::information(this,"提示","当前购物车为空！",QMessageBox::Yes);
        return;
    }
    int decreased = sql.value("tnum").toInt();
    int sid = sql.value("tstore").toInt();
    int gid = sql.value("tgoods").toInt();
    QString cat = sql.value("tcat").toString();


    sql.prepare("SELECT sum(tprice * tnum)as total FROM tb_trolly WHERE tuser ="+ QString::number(m_uid));
    sql.exec();
    sql.first();
    float total = sql.value("total").toFloat()/1000;
    float profit = total*1000;
    sql.prepare("SELECT upoints FROM tb_user WHERE uid ="+ QString::number(m_uid));
    sql.exec();
    sql.first();
    float current = sql.value("upoints").toFloat();
    current = total + current;
    float currentPoints = current;
    sql.prepare("UPDATE tb_user SET upoints = :current WHERE uid ="+ QString::number(m_uid));
    sql.bindValue(":current",current);
    sql.exec();
    QMessageBox::information(this,"提示","购买成功！您获得了"+QString::number(total)+"积分，共计"+QString::number(current)+"分！",QMessageBox::Yes);

    sql.prepare("SELECT sprofit FROM tb_store WHERE sid = :sid");
    sql.bindValue(":sid",sid);
    sql.exec();
    sql.first();
    current = sql.value("sprofit").toFloat();
    current += profit;
    sql.prepare("UPDATE tb_store SET sprofit = :current WHERE sid = :sid");
    sql.bindValue(":current",current);
    sql.bindValue(":sid",sid);
    sql.exec();

    if(currentPoints>20 && !m_isVIP)
        QMessageBox::information(this,"提示","当前您的账户可升级为VIP，请前往用户中心升级！",QMessageBox::Yes);


qDebug()<<m_isVIP;
    sql.prepare("SELECT num FROM tb_goodsinstore WHERE sid = :sid AND gid = :gid");
    sql.bindValue(":sid",sid);
    sql.bindValue(":gid",gid);
    sql.exec();
    sql.first();
    int currentNum = sql.value("num").toInt();
qDebug()<<"goodsinstore = " <<currentNum;
    currentNum = currentNum - decreased;
    sql.prepare("UPDATE tb_goodsinstore SET num = :currentNum WHERE sid = :sid AND gid = :gid");
    sql.bindValue(":currentNum",currentNum);
    sql.bindValue(":sid",sid);
    sql.bindValue(":gid",gid);
    sql.exec();
qDebug()<<"goodsinstore = " <<currentNum;
    sql.prepare("SELECT gnum FROM tb_goods WHERE gid = :gid");
    sql.bindValue(":gid",gid);
    sql.exec();
    sql.first();
    currentNum = sql.value("gnum").toInt();
qDebug()<<"goods = " <<currentNum;
    currentNum = currentNum - decreased;
    sql.prepare("UPDATE tb_goods SET gnum = :currentNum WHERE gid = :gid");
    sql.bindValue(":currentNum",currentNum);
    sql.bindValue(":gid",gid);
    sql.exec();
qDebug()<<"goods = " <<currentNum;
    sql.prepare("SELECT cnum,cprofit FROM tb_goods_cat WHERE cname = :cat");
    sql.bindValue(":cat",cat);
    sql.exec();
    sql.first();
    current = sql.value("cprofit").toFloat();
    current+= profit;
    currentNum = sql.value("cnum").toInt();
qDebug()<<"cat = " <<currentNum;
    currentNum = currentNum - decreased;
    sql.prepare("UPDATE tb_goods_cat SET cnum = :currentNum,cprofit = :current WHERE cname = :cat");
    sql.bindValue(":currentNum",currentNum);
    sql.bindValue(":current",current);
    sql.bindValue(":cat",cat);
    sql.exec();
qDebug()<<"cat = " <<currentNum;
    sql.prepare("DELETE FROM tb_trolly WHERE tuser = "+ QString::number(m_uid));
    sql.exec();
}

